﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using DddCms.Domain.Repository;
using DddCms.Domain.Model;

namespace DddCms.Infrastructure.Repository
{
    /// <summary>
    /// A proxy init repository item
    /// </summary>
    public class Repository : IRepository
    {
        [Dependency]
        public IDataContext DataContext { get; set; }

        [Dependency]
        public IUnityContainer UnityContainer { get; set; }


        public T init<T, K>(ref T prop) where T : IRepositoryItem<K>
        {
            if (prop == null)
            {
                prop = UnityContainer.Resolve<T>();
                prop.setDataContext(DataContext);
            }
            return prop;
        }
        
        private IUserRepository userReporitory;
        public IUserRepository Users
        {
            get { return init<IUserRepository, User>(ref userReporitory); }
        }

        private IRoleRepositoy roleRepositoy;
        public IRoleRepositoy Roles
        {
            get { return init<IRoleRepositoy, Role>(ref roleRepositoy); }
        }
    }
}
